package com.hzncc.scaffold.controller.Rear.teachingClassroom;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.*;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.CourseMapper;
import com.hzncc.scaffold.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-09 15:49
 * @Description: 人才方案
 */
@RestController
@RequestMapping("/manage/trainingProgram")
@Api(tags = "教学课堂管理-人才方案")
public class TalentPlanController {

    @Autowired
    TrainingprogramService trainingprogramService;

    @Autowired
    UserService userService;

    @Autowired
    MajorService majorService;

    @Autowired
    TrainingprogramcourseService trainingprogramcourseService;

    @Autowired
    CourseService courseService;

    @Autowired
    CoursemajorService coursemajorService;

    @Autowired
    CourseMapper courseMapper;


    @GetMapping("/list")
    @ApiOperation("获取人才方案列表")
    public ResultT list(Integer pageNum, Integer pageSize, Integer edPhase, String keyword) {
        if (edPhase != null && edPhase != 0 && edPhase != 1 && edPhase != 2) {
            return ResultT.fail("数据错误");
        }

        ArrayList<TrainingProgramListVo> trainingProgramListVos = new ArrayList<>();

        LambdaQueryWrapper<Trainingprogram> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotEmpty(keyword), Trainingprogram::getTitle, keyword);
        lqw.eq(edPhase != null, Trainingprogram::getEdPhase, edPhase);
        lqw.eq(Trainingprogram::getIsDelete, 0);

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Trainingprogram> trainingprogramList = trainingprogramService.list(lqw);
        for (Trainingprogram trainingprogram : trainingprogramList) {
            TrainingProgramListVo trainingProgramListVo = BeanUtil.toBean(trainingprogram, TrainingProgramListVo.class);

            Publisher publisher = new Publisher();
            if (trainingprogram.getPublisherId() != null) {
                publisher.setId(trainingprogram.getPublisherId());
                if (userService.getById(trainingprogram.getPublisherId()) != null) {
                    publisher.setName(userService.getById(trainingprogram.getPublisherId()).getName());
                }
            }
            Major major = new Major();
            Integer majorId = trainingprogram.getMajorId();
            if (majorService.getById(majorId) != null) {
                major = majorService.getById(majorId);
            }

            trainingProgramListVo.setMajor(major);
            trainingProgramListVo.setPublisher(publisher);
            trainingProgramListVos.add(trainingProgramListVo);
        }

//        PageInfo<TrainingProgramListVo> info = new PageInfo<>();
//        info.setList(trainingProgramListVos);
//        info.setTotal(new PageInfo<>(trainingprogramList).getTotal());
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",trainingProgramListVos);
        map.put("count",new PageInfo<>(trainingprogramList).getTotal());
        return ResultT.success(map);
    }


    @PostMapping("/stayAtTop")
    @ApiOperation("置顶人才方案")
    public ResultT stayAtTop(@RequestBody TalentPlanStayAtTopVo talentPlanStayAtTopVo) {

        Integer programId = talentPlanStayAtTopVo.getProgramId();
        Integer stayAtTop = talentPlanStayAtTopVo.getStayAtTop();

        ResultT resultT = trainingprogramService.stayAtTop(programId, stayAtTop);
        return resultT;
    }


    @GetMapping("/detail")
    @ApiOperation("获取人才方案详情")
    public ResultT detail(Integer programId) {

//        Integer programId = programIdVo.getProgramId();

        if (trainingprogramService.getById(programId) == null) {
            return ResultT.fail("数据错误");
        }
        Trainingprogram trainingprogram = trainingprogramService.getById(programId);
        TalentPlanDetailVo talentPlanDetailVo = BeanUtil.toBean(trainingprogram, TalentPlanDetailVo.class);

        Major major = new Major();
        if (majorService.getById(trainingprogram.getMajorId()) != null) {
            major = majorService.getById(trainingprogram.getMajorId());
        }
        ArrayList<ProgramCourseVo> programCourseVos = new ArrayList<>();

        LambdaQueryWrapper<Trainingprogramcourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Trainingprogramcourse::getTrainingProgramId, trainingprogram.getId());
        List<Trainingprogramcourse> trainingprogramcourseList = trainingprogramcourseService.list(lqw);

        for (Trainingprogramcourse trainingprogramcourse : trainingprogramcourseList) {
            Integer courseId = trainingprogramcourse.getCourseId();
            if (courseService.getById(courseId) != null) {
                Course course = courseService.getById(courseId);
                ProgramCourseVo programCourseVo = BeanUtil.toBean(course, ProgramCourseVo.class);


                ArrayList<Major> majors = new ArrayList<>();

                LambdaQueryWrapper<Coursemajor> lqww = new LambdaQueryWrapper<>();
                lqww.eq(Coursemajor::getCourseId, courseId);
                List<Coursemajor> coursemajors = coursemajorService.list(lqww);

                for (Coursemajor coursemajor : coursemajors) {
                    major = majorService.getById(coursemajor.getMajorId());
                    majors.add(major);
                }

                programCourseVo.setMajors(majors);

                programCourseVos.add(programCourseVo);
            }
        }

        talentPlanDetailVo.setProgramCourse(programCourseVos);
        talentPlanDetailVo.setMajor(major);
        return ResultT.success(talentPlanDetailVo);
    }


    /**
     * 修改人才方案
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改人才方案")
    public ResultT update(@RequestBody UpdateTrainingProgramVo updateTrainingProgramVo) {
        Trainingprogram trainingprogram = BeanUtil.toBean(updateTrainingProgramVo, Trainingprogram.class);
        trainingprogram.setId(updateTrainingProgramVo.getProgramId());
        List<Integer> courseIdList = updateTrainingProgramVo.getCourseId();
        if (CollUtil.isNotEmpty(courseIdList)) {
            for (Integer courseId : courseIdList) {
                LambdaQueryWrapper<Trainingprogramcourse> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Trainingprogramcourse::getCourseId, courseId);
                List<Trainingprogramcourse> trainingprogramcourseList = trainingprogramcourseService.list(lqw);
                for (Trainingprogramcourse trainingprogramcourse : trainingprogramcourseList) {
                    trainingprogramcourse.setTrainingProgramId(updateTrainingProgramVo.getProgramId());
                    trainingprogramcourseService.updateById(trainingprogramcourse);
                }
            }
        }
        boolean update = trainingprogramService.updateById(trainingprogram);
        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    /**
     * 新增人才方案
     *
     * @param
     * @return
     */
    @PostMapping("/commit")
    @ApiOperation("新增人才方案")
    public ResultT commit(@RequestBody CommitTrainingProgramVo commitTrainingProgramVo) {
        ResultT resultT = trainingprogramService.commit(commitTrainingProgramVo);

        if (resultT.getCode() == 200) {
            Object programId = resultT.getData();
            List<Integer> courseIdList = commitTrainingProgramVo.getCourseId();

            if (CollUtil.isNotEmpty(courseIdList)) {
                for (Integer courseId : courseIdList) {
                    Trainingprogramcourse trainingprogramcourse = new Trainingprogramcourse();
                    trainingprogramcourse.setCourseId(courseId);
                    trainingprogramcourse.setTrainingProgramId((Integer) programId);

                    trainingprogramcourseService.save(trainingprogramcourse);

                    return ResultT.success();
                }
            }
        }
        return resultT;
    }

    @PostMapping("/delete")
    @ApiOperation("删除人才方案")
    public ResultT delete(@RequestBody ProgramIdVo programIdVo) {

        Integer programId = programIdVo.getProgramId();

        ResultT resultT = trainingprogramService.deleteTrainingprogram(programId);
        if (programId == null) {
            return ResultT.fail("数据错误");
        }
        LambdaQueryWrapper<Trainingprogramcourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Trainingprogramcourse::getTrainingProgramId, programId);
        trainingprogramcourseService.remove(lqw);
        return resultT;
    }

    @PostMapping("/publish")
    @ApiOperation("人才方案发布")
    public ResultT publish(@RequestBody ProgramIdVo programIdVo) {

        Integer programId = programIdVo.getProgramId();

        if (trainingprogramService.getById(programId) == null) {
            return ResultT.fail("数据错误");
        }
        Trainingprogram trainingprogram = trainingprogramService.getById(programId);
        trainingprogram.setStayAtTop(1);
        trainingprogram.setPubTime(DateUtil.now());


        //todo 存入发布人信息
        Integer publisherId = StpUtil.getLoginIdAsInt();
        trainingprogram.setPublisherId(publisherId);


        boolean update = trainingprogramService.updateById(trainingprogram);
        if (update) {
            return ResultT.success("发布成功");
        } else {
            return ResultT.fail("发布失败");
        }
    }

    //    @GetMapping("/courseList")
//    @ApiOperation("获取课程列表")
//    public ResultT courseList(Integer pageNum, Integer pageSize, String keyword, Integer majorId) {
//
//        ArrayList<ProgramCourseVo> programCourseVos = new ArrayList<>();
//        LambdaQueryWrapper<Course> lqw = new LambdaQueryWrapper<>();
//        lqw.like(StrUtil.isNotEmpty(keyword), Course::getTitle, keyword);
//        lqw.orderByDesc(Course::getId);
//        lqw.eq(Course::getIsDelete, 0);
//        lqw.eq(Course::getStatus, 1);
//
//        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
//            PageHelper.startPage(pageNum, pageSize);
//        }
//        List<Course> courseList = courseService.list(lqw);
//        for (Course course : courseList) {
//            ProgramCourseVo programCourseVo = BeanUtil.toBean(course, ProgramCourseVo.class);
//
//            LambdaQueryWrapper<Coursemajor> lqww = new LambdaQueryWrapper<>();
//            lqww.eq(Coursemajor::getCourseId, course.getId());
//            List<Coursemajor> coursemajorList = coursemajorService.list(lqww);
//
//            ArrayList<Major> majors = new ArrayList<>();
//            ArrayList<Integer> majorIdList = new ArrayList<>();
//
//            if (CollUtil.isNotEmpty(coursemajorList)) {
//                for (Coursemajor coursemajor : coursemajorList) {
//                    if (majorService.getById(coursemajor.getMajorId()) != null) {
//                        Major major = majorService.getById(coursemajor.getMajorId());
//                        majorIdList.add(major.getId());
//                        majors.add(major);
//                    }
//                }
//            }
//            programCourseVo.setMajors(majors);
//            if (majorId != null) {
//                if (!CollUtil.contains(majorIdList, majorId)) {
//                    continue;
//                }
//            }
//            programCourseVos.add(programCourseVo);
//        }
//        return ResultT.success(programCourseVos);
//    }
    @GetMapping("/courseList")
    @ApiOperation("课程管理列表")
    public ResultT list(Integer pageNum, Integer pageSize, String keyWord, Integer majorId) {
        ArrayList<CourseListVo> courseListVos = new ArrayList<>();

        QueryWrapper<Course> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyWord)) {
            qw.like("`course`.title", keyWord);
        }
        if (majorId != null) {
            qw.eq("`majors`.`id`", majorId);
        }
        qw.eq("course.isDelete", 0);
        qw.eq("`majors`.`pid`", 0);
        //todo
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }

        List<Course> courseList = courseMapper.listVo(qw);
        for (Course course : courseList) {
            CourseListVo courseListVo = BeanUtil.toBean(course, CourseListVo.class);

            ArrayList<Major> majorList = new ArrayList<>();
            ArrayList<Integer> idList = new ArrayList<>();
            Publisher publisher = new Publisher();

            LambdaQueryWrapper<Coursemajor> lqww = new LambdaQueryWrapper<>();
            lqww.eq(Coursemajor::getCourseId, course.getId());
            List<Coursemajor> majorIdList = coursemajorService.list(lqww);
            if (CollUtil.isNotEmpty(majorIdList)) {
                for (Coursemajor coursemajor : majorIdList) {
                    if (majorService.getById(coursemajor.getMajorId()) != null) {
                        Major major = majorService.getById(coursemajor.getMajorId());
                        idList.add(coursemajor.getMajorId());
                        majorList.add(major);
                    }
                }
            }
            if (userService.getById(course.getPublisherId()) != null) {
                User user = userService.getById(course.getPublisherId());
                publisher = BeanUtil.toBean(user, Publisher.class);
            }

            courseListVo.setPublisher(publisher);
            courseListVo.setMajors(majorList);


            courseListVos.add(courseListVo);
        }

        PageInfo<CourseListVo> info = new PageInfo<>();
        info.setList(courseListVos);
        info.setTotal(new PageInfo<>(courseList).getTotal());

        return ResultT.success(info);
    }

}
